package cn.seapon.talker.common;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.integer;

/** 登录后返回的好友列表和在线情况的类
 * @author 作者 E-mail: 
 * @version 创建时间：2015年1月3日 下午10:32:28 
 * 类说明 
 */
public class FriendListInfomation {
	private static final String TAG = "||___FriendListInfomation___||";
	
	private ArrayList<FriendInfomation> friendInfos = new ArrayList<FriendInfomation>();
	private int count = 0;
	
	public JSONArray getJsonArray() {
		JSONArray jsonArray = new JSONArray();
		for (int i = 0; i < friendInfos.size(); i++) {
			JSONObject object = new JSONObject();
			try {
				object.put("id", friendInfos.get(i).getId());
				object.put("nickname", friendInfos.get(i).getNickname());
				object.put("remarkname", friendInfos.get(i).getRemarkname());
				object.put("online", friendInfos.get(i).getOnline());
				object.put("groupid", friendInfos.get(i).getGroupid());
				object.put("lastIOnline", friendInfos.get(i).getLastIOnline());
				object.put("signature", friendInfos.get(i).getSignature());
				object.put("avatar",  friendInfos.get(i).getAvatar());
				jsonArray.put(object);//json对象加入JSON数组
			} catch (JSONException e) {
				e.printStackTrace();
				return null;
			}
		}
		return jsonArray;
	}
	
	public String getJsonString() {
		return getJsonArray().toString();
	}
	
	public static FriendListInfomation getObjectFromJson(JSONArray jsonArray){
		FriendListInfomation infos = new FriendListInfomation();
		try {
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject object = (JSONObject) jsonArray.get(i);
				
				String id = object.getString("id");
				String nickname = object.getString("nickname");
				String remarkname = object.getString("remarkname");
				boolean online = object.getBoolean("online");
				int groupid = object.getInt("groupid");
				int lastIOnline = object.getInt("lastIOnline");
				String signature = object.getString("signature");
				String avatar = object.getString("avatar");
				
				FriendInfomation info = new FriendInfomation();
				info.setId(id);
				info.setLastIOnline(lastIOnline);
				info.setNickname(nickname);
				info.setGroupid(groupid);
				info.setOnline(online);
				info.setRemarkname(remarkname);
				info.setAvatar(avatar);
				info.setSignature(signature);
				
				infos.add(info);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return infos;
	}

	
	public int getCount(){
		return count;
	}
	
	public FriendInfomation getAt(int index) {
		return friendInfos.get(index);
	}
	
	public void add(FriendInfomation infomation){
		friendInfos.add(infomation);//永远返回TRUE
		count++;
	}
	
	public String[] getIds(){
		String[] array = new String[count];
		for (int i = 0; i < friendInfos.size(); i++) {
			array[i] = friendInfos.get(i).getId();
		}
		return array;
	}
	
	public FriendInfomation getInfoById(String id){
		if ((id == null) || "".equals(id) ) {
			return null;
		}
		for (int i = 0; i <count; i++) {
			FriendInfomation info = friendInfos.get(i);
			if (id.equals( info.getId().trim())) {
				return info;
			}
		}
		return null;
	}

	
	public FriendInfomation[] getAllInfo(){
		FriendInfomation[] friendInfomations = new  FriendInfomation[count];
		for (int i = 0; i < count; i++) {
			friendInfomations[i] = friendInfos.get(i);
		}
		return friendInfomations;
	}
	
	
	public static class FriendInfomation implements Serializable{
		//好友ID
		private String id = "";
		//好友昵称
		private String nickname = "";
		//好友备注名称
		private String remarkname = "";
		//好友ID对应的在线情况
		private boolean online = false;
		//好友好友上次登录时间
		private int lastIOnline = 0;
		//好友所属组号
		private int groupid = 0;
		//好友签名
		private String signature = "";
		
		public String getSignature() {
			return signature;
		}

		public void setSignature(String signature) {
			this.signature = signature;
		}

		public String getAvatar() {
			return avatar;
		}

		public void setAvatar(String avatar) {
			this.avatar = avatar;
		}
		//好友头像
		private String avatar = "";

		public String getId() {
			return id;
		}
		
		public FriendInfomation() {
		}

		public void setId(String id) {
			this.id = id;
		}
		public String getNickname() {
			return nickname;
		}
		public int getGroupid() {
			return groupid;
		}

		public void setGroupid(int groupid) {
			this.groupid = groupid;
		}
		public void setNickname(String nickname) {
			this.nickname = nickname;
		}
		public String getRemarkname() {
			return remarkname;
		}
		public void setRemarkname(String remarkname) {
			this.remarkname = remarkname;
		}
		public boolean getOnline() {
			return online;
		}
		public void setOnline(boolean online) {
			this.online = online;
		}
		public int getLastIOnline() {
			return lastIOnline;
		}
		public void setLastIOnline(int lastIOnline) {
			this.lastIOnline = lastIOnline;
		}
		
	}


}
